% To plot figures for Chapter 1

clear all;
clc;

linestyles = cellstr(char('-',':','-.','--','-',':','-.','--','-',':','-',':',...
'-.','--','-',':','-.','--','-',':','-.'));
Markers=['o','+','s','d','*','x','v','^','<','>','p','h','.',...
'+','*','o','x','^','<','h','.','>','p','s','d','v',...
'o','x','+','*','s','d','v','^','<','>','p','h','.'];
%MarkerEdgeColors=jet(4);  % n is the number of different items you have
R = cellstr(char('1.5','2','2.5','3','3.5','4','4.5'));
Y = cellstr(char('1998','1999','2000','2001','2002','2003','2004','2005','2006','2007','2008','2009'));
SG = cellstr(char('Sample 1: [$50k,+)','Sample 2A: [$50k, 250k)','Sample 2B: [$250k, 1000k)','Sample 2C: [$1000k, +)'));
S = cellstr(char('Business Services','Construction','Manufacture','Non-Business Services', 'Other',	'Resources','Retail','Supplier Premises','Tourism','Transport+Storage','Wholesale'));
G = cellstr(char('Alberta','British Columbia','Manitoba','New Brunswick','Newfoundland & Labrador','Northwest Territories','Nova Scotia','Ontario','Prince Edward Island','Quebec','Saskatchewan','Yukon'));

%% Figure 2 Percentage Composition of Sample Groups
loanComp = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','ObligorYrs_%','K4:L7');
figure
%title('Percentage Composition of Sample Groups by Obligor Years and Loan Amount');
bar(loanComp); colormap(summer)
set(gca, 'YTickMode','manual') % set unit to percentage
set(gca, 'YTickLabel',num2str(100.*get(gca,'YTick')','%g%%')) % set unit to percentage
set(gca,'XTickLabel',SG);
xlabel('Sample Groups');
ylabel('Percentage on Whole Sample');
legend('Obligor Years %','Est. Loan Amount %');
set(gca,'YGrid','on');



%% Figure 3 Average Rating Distribution
Linecolors=jet(4);
RatingNums = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','Ratings','B13:E19');
figure
%title('Average Rating Distribution - Numbers');
for i = 1:4
hold on
plot (RatingNums(:,i),[linestyles{1} Markers(i)],'LineWidth',2,'Color',Linecolors(i,:),'MarkerSize',10);
end
hold off
set(gca,'XTickLabel',R);
xlabel('Ratings');
ylabel('Average Number of Obligors');
legend(SG,'Location','NorthWest');
grid on;

RatingPcg = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','Ratings','B24:E30');
figure
%title('Average Rating Distribution - percentage');
for i = 1:4
hold on
plot (RatingPcg(:,i),[linestyles{1} Markers(i)],'LineWidth',2,'Color',Linecolors(i,:),'MarkerSize',10);
end
hold off
set(gca,'XTickLabel',R);
xlabel('Ratings');
ylabel('Percentage of Obligors on Whole Sample ');
legend(SG,'Location','NorthWest');
grid on;


%% Figure 4 Rating Distribution Over Time
RateDistTime = zeros(4, length(R), length(Y));
% dimension 1-4 represents sample group SG
RateDistTime(1,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','Rate_Time','s5:ad11');
RateDistTime(2,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','Rate_Time','s16:ad22');
RateDistTime(3,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','Rate_Time','s27:ad33');
RateDistTime(4,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','Rate_Time','s38:ad44');

Linecolors=jet(length(Y));
% To create 4 seperated figures
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% for i = 1:length(SG)
%     figure
%     
%     for j = 1:length(Y)
%         hold on
%         plot (RateDistTime(i,:,j),[linestyles{1} Markers(j)],'LineWidth',2,'Color',Linecolors(j,:),'MarkerSize',10);
%     end
%     
%     hold off
%     title(SG(i));
%     set(gca,'XTickLabel',R);
%     xlabel('Ratings');
%     ylabel('Number of Obligors ');
%     legend(Y,'Location','NorthEastOutside')
%     grid on
% end

% To create one window with 4 figures
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
for i = 1: length(SG)
    subplot(2,2,i);
    for j = 1:length(Y)
        hold on
        plot (RateDistTime(i,:,j),[linestyles{1} Markers(j)],'LineWidth',2,'Color',Linecolors(j,:),'MarkerSize',10);
    end
    hold off
    title(SG(i));
    set(gca,'XTickLabel',R);
    xlabel('Ratings');
    ylabel('Number of Obligors')    
    grid on
end
legend(Y,'Location','NorthEastOutside')


% To see the effect of bar
% for i = 1:1%length(SG)
%     x(:,:) = RateTime(i,:,:);
%     bar(x);
% end


%% Figure 5 Rating Evolution Over Time
RateTime = zeros(4, length(Y), length(R));
% dimension 1-4 represents sample group SG
RateTime(1,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','Rate_Time','ah5:an16');
RateTime(2,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','Rate_Time','ah21:an32');
RateTime(3,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','Rate_Time','ah37:an48');
RateTime(4,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','Rate_Time','ah53:an64');

Linecolors=jet(length(R));

% To create 4 seperated figures
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% for i = 1:1%length(SG)
%     figure
%     
%     for j = 1:length(R)
%         hold on
%         plot (RateTime(i,:,j),[linestyles{1} Markers(j)],'LineWidth',2,'Color',Linecolors(j,:),'MarkerSize',10);
%     end
%     hold off
%     
%     title(SG(i));
%     set(gca,'XLim',[1 12]);
%     %set(gca,'XTick',[1:12])
%     set(gca,'XTickLabel',Y);
%     xlabel('Years');
%     ylabel('Number of Obligors ');
%     legend(R,'Location','NorthEastOutside')
%     grid on
% end

% To create one window with 4 figures
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
for i = 1: length(SG)
    subplot(2,2,i);
    for j = 1:length(R)
        hold on
        plot (RateTime(i,:,j),[linestyles{1} Markers(j)],'LineWidth',2,'Color',Linecolors(j,:),'MarkerSize',10);
    end
    hold off
    title(SG(i));
    set(gca,'XLim',[1 12]);
    set(gca,'XTickLabel',Y);
    xlabel('Years');
    ylabel('Number of Obligors')    
    legend(R,'Location','NorthEastOutside')
    grid on
end


%% Figure 6 Industry Distribution

Sectors = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','Sector','o5:r15');

figure
bar ( Sectors(:,2:length(SG)) ); colormap(summer);
hold on
plot (Sectors(:,1), [linestyles{3} Markers(1)],'LineWidth',3,'MarkerSize',8,'markerfacecolor','r');
%set(gca,'XTickLabel',S);
xticklabel_rotate(1:length(S),45,S);
set(gca, 'YTickMode','manual') % set unit to percentage
set(gca, 'YTickLabel',num2str(100.*get(gca,'YTick')','%g%%')) % set unit to percentage
xlabel('Industry Sectors');
ylabel('Percentage of Obligors');
legend('Sample 2A: [$50k, 250k)','Sample 2B: [$250k, 1000k)','Sample 2C: [$1000k, +)','Sample 1: [$50k, +)');
grid on

%% Figure 7 Industry Distribution By Time

SectorTime = zeros(4, length(Y), length(S));
% dimension 1-4 represents sample group SG
SectorTime(1,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','SectorTime','h20:r31');
SectorTime(2,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','SectorTime','h33:r44');
SectorTime(3,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','SectorTime','h46:r57');
SectorTime(4,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','SectorTime','h59:r70');

Linecolors=jet(length(S));
% To create 4 seperated figures
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% for i = 1:length(SG)
%     figure
%     
%     for j = 1:length(S)
%         hold on
%         plot (SectorTime(i,:,j),[linestyles{1} Markers(j)],'LineWidth',2,'Color',Linecolors(j,:),'MarkerSize',10);
%     end
%     hold off
%     
%     title(SG(i));
%     set(gca,'XLim',[1 12]);
%     set(gca,'XTickLabel',Y);
%     xlabel('Years');
%     ylabel('Number of Obligors ');
%     legend(S,'Location','NorthWest')
%     grid on
% end

% To create one window with 4 figures
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
for i = 1: length(SG)
    subplot(2,2,i);
    for j = 1:length(S)
        hold on
        plot (SectorTime(i,:,j),[linestyles{1} Markers(j)],'LineWidth',2,'Color',Linecolors(j,:),'MarkerSize',10);
    end
    hold off
    title(SG(i));
    set(gca,'XLim',[1 12]);
    set(gca,'XTickLabel',Y);
    xlabel('Years');
    ylabel('Number of Obligors')    
    legend(S,'Location','NorthWest')
    grid on
end



% To see the effect of bar
% for i = 1:1%length(SG)
%     x(:,:) = SectorTime(i,:,:);
%     bar(x);
% end

%% Figure 8 Rating Distribution by Industry

RateSectors = zeros(4, length(R), length(S));
% dimension 1-4 represents sample group SG
RateSectors(1,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','RateSector','ag5:aq11');
RateSectors(2,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','RateSector','ag19:aq25');
RateSectors(3,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','RateSector','ag32:aq38');
RateSectors(4,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','RateSector','ag45:aq51');

Linecolors=jet(length(S));

% % To create 4 seperated figures
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% figure
% for i = 1:length(SG)
%     figure
%     
%     for j = 1:length(S)
%         hold on
%         plot (RateSectors(i,:,j),[linestyles{1} Markers(j)],'LineWidth',2,'Color',Linecolors(j,:),'MarkerSize',10);
%     end
%     hold off
%     
%     title(SG(i));
%     set(gca,'XTickLabel',R);
%     xlabel('Ratings');
%     ylabel('Percentage of Obligors ');
%     legend(S,'Location','NorthWest')
%     grid on
% end

% To create one window with 4 figures
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
for i = 1: length(SG)
    subplot(2,2,i);
    for j = 1:length(S)
        hold on
        plot (RateSectors(i,:,j),[linestyles{1} Markers(j)],'LineWidth',2,'Color',Linecolors(j,:),'MarkerSize',10);
    end
    hold off
    title(SG(i));   
    set(gca,'XTickLabel',R);
    xlabel('Ratings');
    ylabel('Percentage of Obligors ')    
    legend(S,'Location','NorthWest')
    grid on
end



%% Figure 9 Geography Distribution

Geo = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','Geo','o4:r15');

figure
bar ( Geo(:,2:length(SG)) ); colormap(summer);
hold on
plot (Geo(:,1), [linestyles{3} Markers(1)],'LineWidth',3,'MarkerSize',8,'markerfacecolor','r');
set(gca,'XTickLabel',G);
set(gca,'XLim',[0 length(G)+1]);
xticklabel_rotate(1:length(G),30,G);
set(gca, 'YTickMode','manual') % set unit to percentage
set(gca, 'YTickLabel',num2str(100.*get(gca,'YTick')','%g%%')) % set unit to percentage
xlabel('Provinces');
ylabel('Percentage of Obligors');
legend('Sample 2A: [$50k, 250k)','Sample 2B: [$250k, 1000k)','Sample 2C: [$1000k, +)','Sample 1: [$50k, +)');
grid on

%% Figure 10 Geography Distribution By Time

GeoTime = zeros(4, length(Y), length(G));
% dimension 1-4 represents sample group SG
GeoTime(1,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','GeoTime','h19:s30');
GeoTime(2,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','GeoTime','h32:s43');
GeoTime(3,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','GeoTime','h45:s56');
GeoTime(4,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','GeoTime','h58:s69');



%the code commented is for percentage figures
Linecolors=jet(length(G));

% % To create 4 seperated figures
% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% for i = 1:length(SG)
%     figure
%     %xx(:,:) = GeoTime(i, :,:);
%     for j = 1:length(G)
%         hold on
%         plot (GeoTime(i,:,j),[linestyles{1} Markers(j)],'LineWidth',2,'Color',Linecolors(j,:),'MarkerSize',10);
%         %plot (xx(:,j)./sum(xx,2),[linestyles{1} Markers(j)],'LineWidth',2,'Color',Linecolors(j,:),'MarkerSize',10);
%     end
%     hold off
%     
%     title(SG(i));
%     set(gca,'XLim',[1 length(Y)]);
%     set(gca,'XTickLabel',Y);
%     xlabel('Years');
%     ylabel('Number of Obligors ');
%     legend(G,'Location','NorthWest')
%     grid on
% end
% clear xx

% To create one window with 4 figures
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
for i = 1: length(SG)
    subplot(2,2,i);
    for j = 1:length(G)
        hold on
        plot (GeoTime(i,:,j),[linestyles{1} Markers(j)],'LineWidth',2,'Color',Linecolors(j,:),'MarkerSize',10);
    end
    hold off
    title(SG(i));  
    set(gca,'XLim',[1 length(Y)]);
    set(gca,'XTickLabel',Y);
    xlabel('Years');
    ylabel('Number of Obligors ')    
    legend(G,'Location','NorthWest')
    grid on
end


%% Figure 11 Rating Distribution by Provinces

RateGeo = zeros(4, length(R),length(G));
% dimension 1-4 represents sample group SG
RateGeo(1,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','RateGeo','h5:s11');
RateGeo(2,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','RateGeo','h18:s24');
RateGeo(3,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','RateGeo','h31:s37');
RateGeo(4,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','RateGeo','h44:s50');

Linecolors=jet(length(G));

% % To create 4 seperated figures
% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% for i = 1:length(SG)
%     figure
%     xx(:,:) = RateGeo(i,:,:);
%     for j = 1:length(G)
%         hold on
%         plot (xx(:,j)./sum(xx(:,j)),[linestyles{1} Markers(j)],'LineWidth',2,'Color',Linecolors(j,:),'MarkerSize',10);
%     end
%     hold off
%     
%     title(SG(i));
%     set(gca,'XTickLabel',R);
%     xlabel('Ratings');
%     ylabel('Percentage of Obligors ');
%     legend(G,'Location','NorthEastOutside')
%     grid on
% end
% clear xx

% To create one window with 4 figures
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
for i = 1: length(SG)
    subplot(2,2,i);
    xx(:,:) = RateGeo(i,:,:);
    for j = 1:length(G)
        hold on
        plot (xx(:,j)./sum(xx(:,j)),[linestyles{1} Markers(j)],'LineWidth',2,'Color',Linecolors(j,:),'MarkerSize',10);
    end
    hold off
    title(SG(i));  
    set(gca,'XTickLabel',R);
    xlabel('Ratings');
    ylabel('Percentage of Obligors ')    
    legend(G,'Location','NorthWest')
    grid on
end
clear xx

%% Figure 12 Default Freqency
DNum = zeros(4, length(Y),2);
DNum(1,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','ObligorYrs_Num','g6:h17');
DNum(2,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','ObligorYrs_Num','g25:h36');
DNum(3,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','ObligorYrs_Num','g43:h54');
DNum(4,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','ObligorYrs_Num','g61:h72');

Linecolors=jet(length(SG));
PD = zeros (length(Y),4);
figure
for i = 1:length(SG)
    
    xx(:,:) = DNum(i,:,1); % 1st colunm is healthy num
    yy(:,:) = DNum(i,:,2); % 2nd colunm is default num
    PD(:,i) =  yy ./ xx;
    hold on
    plot (PD(:,i),[linestyles{1} Markers(i)],'LineWidth',2,'Color',Linecolors(i,:),'MarkerSize',10);
        
end
% to check if sample 2b + 2c as a group, the PD shape
% xx(:,:) = DNum(3,:,1)+ DNum(4,:,1);
% yy(:,:) = DNum(3,:,2)+ DNum(4,:,2);
% z = yy ./ xx;
% plot (z,[linestyles{1} Markers(5)],'LineWidth',2,'MarkerSize',10);
hold off
set(gca,'XLim', [1, length(Y)]);
set(gca,'XTickLabel',Y);
xlabel('Years');
ylabel('Default Freqency ');
legend(SG)
grid on

% the min and max PD
L = min(min(PD)); % = 0.0461
H = max(max(PD)); % = 0.1109
clear xx yy

%% Figure 13 Default Freqency by Rating
PDRate = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','PDRate','k5:n11');
figure
bar ( PDRate(:,2:length(SG)) ); colormap(summer);
hold on
plot (PDRate(:,1), [linestyles{3} Markers(1)],'LineWidth',3,'MarkerSize',8,'markerfacecolor','r');
set(gca,'XTickLabel',R);
set(gca, 'YTickMode','manual') % set unit to percentage
set(gca, 'YTickLabel',num2str(100.*get(gca,'YTick')','%g%%')) % set unit to percentage
xlabel('Rating');
ylabel('Default Frequency');
legend('Sample 2A: [$50k, 250k)','Sample 2B: [$250k, 1000k)','Sample 2C: [$1000k, +)','Sample 1: [$50k, +)');
grid on


%% Figure 14 Default Freqency by Rating and Time

PDRateTime_num = zeros(4, length(Y), 2*length(R));
PDRateTime_num(1,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','PDRateTime','h7:u18');
PDRateTime_num(2,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','PDRateTime','h27:u38');
PDRateTime_num(3,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','PDRateTime','h46:u57');
PDRateTime_num(4,:,:) = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','PDRateTime','h65:u76');

Linecolors=jet(length(R));

% To create 4 seperated figures
% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% for i = 1: length(SG)
%     
%     xx(:,:) = PDRateTime_num(i,:,1:length(R)); % first 7 colunms is default num
%     yy(:,:) = PDRateTime_num(i,:,length(R)+1:2*length(R)); % last 7 colunms is health num
%     PDRateTime =  xx ./ yy;
%     
%     figure
%     
%     for j = 1: length(R)
%         hold on
%         plot (PDRateTime(:,j),[linestyles{1} Markers(j)],'LineWidth',2,'Color',Linecolors(j,:),'MarkerSize',10);
%     end
%     
%     title(SG(i));
%     set(gca, 'xlim',[1,length(Y)]);
%     set(gca,'XTickLabel',Y);
%     set(gca, 'YTickMode','manual') % set unit to percentage
%     set(gca, 'YTickLabel',num2str(100.*get(gca,'YTick')','%g%%')) % set unit to percentage
%     xlabel('Years');
%     ylabel('Default Frequency');
%     legend(R);
%     grid on
% end
% clear xx yy

% To create one window with 4 figures
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
for i = 1: length(SG)
    subplot(2,2,i);
    xx(:,:) = PDRateTime_num(i,:,1:length(R)); % first 7 colunms is default num
    yy(:,:) = PDRateTime_num(i,:,length(R)+1:2*length(R)); % last 7 colunms is health num
    PDRateTime =  xx ./ yy;
    
    for j = 1:length(R)
        hold on
        plot (PDRateTime(:,j),[linestyles{1} Markers(j)],'LineWidth',2,'Color',Linecolors(j,:),'MarkerSize',10);
    end
    hold off
    title(SG(i));  
    set(gca, 'xlim',[1,length(Y)]);
    set(gca,'XTickLabel',Y);
    set(gca, 'YTickMode','manual') % set unit to percentage
    set(gca, 'YTickLabel',num2str(100.*get(gca,'YTick')','%g%%')) % set unit to percentage
    xlabel('Years');
    ylabel('Default Frequency');
    legend(R);
    grid on
end
clear xx yy

%% Figure 15 Default Freqency by Sectors
PDSector = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','PDSector','k5:n15');
figure
bar ( PDSector(:,2:length(SG)) ); colormap(summer);
hold on
plot (PDSector(:,1), [linestyles{3} Markers(1)],'LineWidth',3,'MarkerSize',8,'markerfacecolor','r');
set(gca,'XTickLabel',S);
xticklabel_rotate(1:length(S),30,S);
set(gca, 'YTickMode','manual') % set unit to percentage
set(gca, 'YTickLabel',num2str(100.*get(gca,'YTick')','%g%%')) % set unit to percentage
xlabel('Industry Sector');
ylabel('Default Frequency');
legend('Sample 2A: [$50k, 250k)','Sample 2B: [$250k, 1000k)','Sample 2C: [$1000k, +)','Sample 1: [$50k, +)');
grid on

%% Figure 16 Sector Composition vs. Default Frequency
PDSector = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','PDSector','k5:n15');
Sectors = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','Sector','o5:r15');
figure
for i = 1: length(SG)
    xx(:,1) = Sectors(:,i); % first column is sector percentage
    xx(:,2) = PDSector(:,i); % second column is PD for each sector
    subplot(2,2,i);
    bar(xx); colormap(summer);
    title(SG(i));
    xlabel('Industry Sector');
    ylabel('Percentage')
    set(gca, 'YTickMode','manual') % set unit to percentage
    set(gca, 'YTickLabel',num2str(100.*get(gca,'YTick')','%g%%')) % set unit to percentage    
    legend('Obligor Yrs%','Default%')
    grid on
end
text(0,0,'1:Business Services 2:Construction 3:Manufacture 4:Non-Business Services 5:Other 6:Resources 7:Retail 8:Supplier Premises 9:Tourism 10:Transport+Storage 11:Wholesale','EdgeColor','Black');
clear xx

%% Figure 17 Default Freqency by Georgraphy
PDGeo = xlsread('D:\My Documents\CIRANO\Writing\Ch1_Data Statistics.2011nov9\figdata.xlsx','PDGeo','k5:n16');
figure
bar ( PDGeo(:,2:length(SG)) ); colormap(summer);
hold on
plot (PDGeo(:,1), [linestyles{3} Markers(1)],'LineWidth',3,'MarkerSize',8,'markerfacecolor','r');
%set(gca,'XTick',0:1:length(G)+1);
set(gca,'XLim',[0 length(G)+1]);
set(gca,'XTickLabel',G);
xticklabel_rotate(1:length(G),30,G);
set(gca, 'YTickMode','manual') % set unit to percentage
set(gca, 'YTickLabel',num2str(100.*get(gca,'YTick')','%g%%')) % set unit to percentage
xlabel('Provinces');
ylabel('Default Frequency');
legend('Sample 2A: [$50k, 250k)','Sample 2B: [$250k, 1000k)','Sample 2C: [$1000k, +)','Sample 1: [$50k, +)');
grid on
